using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System.Data;

namespace AdverseEventReportSystem.Repository 
{
    public class AdverseEventNurseRepository : BaseRepository<AdverseEventNurse>
    {

        #region 构造函数

        private static AdverseEventNurseRepository _instance;
        public static AdverseEventNurseRepository GetInstance()
        {
            if (_instance == null)
            {
                _instance = new AdverseEventNurseRepository();
            }
            return _instance;
        }

        #endregion

        #region Add

        public int Add(AdverseEventNurse entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int Id = context.Insert<AdverseEventNurse>("AdverseEventNurse", entity)
                    .AutoMap(x => x.ID)
                    .ExecuteReturnLastId<int>();
            return Id;
        }

        #endregion

        #region Update

        public int Update(AdverseEventNurse entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Update<AdverseEventNurse>("AdverseEventNurse", entity)
                    .AutoMap(x => x.ID)
                    .Where(x => x.ID)
                    .Execute();
            return rowsAffected;
        }

        #endregion

        #region 获取单个实体 通过主键ID

        /// <summary>
        /// 获取单个实体 通过主键ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual AdverseEventNurse GetQuerySingleByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "SELECT * FROM AdverseEventNurse WHERE ID=@0";
            AdverseEventNurse obj = GetQuerySingle(sqlStr, context, objects);
            return obj;
        }

        #endregion

        #region 删除操作  通过ID

        /// <summary>
        /// 删除操作  通过ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "DELETE FROM AdverseEventNurse WHERE ID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 根据不良事件ID删除

        /// <summary>
        /// 根据事件ID删除
        /// </summary>
        /// <param name="eventID">不良事件ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByEventID(int eventID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = eventID;
            string sqlStr = "DELETE FROM AdverseEventNurse WHERE EventID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 获取实体DataTable

        /// <summary>
        /// 根据不良事件ID
        /// </summary>
        /// <param name="typeID">事件类型ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual DataTable GetAdverseEventNuserTable(int eventID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = eventID;
            string sqlStr = "select * from AdverseEventNurse WHERE EventID = @0";
            return GetDataTable(sqlStr, context, objects);
        }

        #endregion
    }
}





